<?php
namespace Admin\Controller;

use Think\Page;
use Common\Base\ControllerBase;
use Common\Logic\Admin\NodeLogic;

class NodeController extends ControllerBase
{
	public $L_node;

	public function _initialize()
	{
		$this->L_node = new NodeLogic();
	}

	//节点列表
	public function node_list()
	{
		$node_pid = (int)I('request.node_pid', 0);
		if($node_pid < 1){ $node_pid = 0; }
		if($node_pid == 0){
			$list = $this->L_node->getNodeList(2, array('node_pid'=>$node_pid), '*', 0, 0, 'px asc');
		}else{
			$list = $this->L_node->getNodeList(2, array('node_pid_path'=>','.$node_pid.','), '*', 0, 0, 'px asc');
            $list = list_to_tree($list, 'node_id', 'node_pid', '_sub', $node_pid); 
		}
		$this->assign('list', $list);
		$this->assign('menuLevel', $this->L_node->menu_level);

		$this->display();
	}

	//节点添加
	public function node_add()
	{
		if(IS_POST){
			$re = $this->L_node->addNode(I('post.'));
			if(false === $re){
                $this->error($this->L_node->getError());
            }else{
                $this->success('操作成功！', U('node/node_list'));
            }
            exit;
		}
		$node_pid = (int)I('get.node_pid');
		$node_level = (int)I('get.node_level');
		$node_pid = ($node_pid > 0) ? $node_pid : 0 ;
		$node_level = ($node_level) ? $node_level : 0 ;
		if($node_pid === 0){
			$this->assign('node_pid', $node_pid);
			$this->assign('node_parents', $this->L_node->menu_level[1]);
			$this->assign('node_level', 1);
		}else{
			$rs = $this->L_node->getNodeList(4, array('node_id'=>$node_pid));
			$this->assign('node_pid', $node_pid);
			$this->assign('node_parents', $rs['node_name']);
		}
		$this->assign('css_styles', $this->L_node->getNodeCss());
		$this->assign('menuLevelSelect', $this->L_node->getMenuLevelSelect($node_level));
		$this->display();
	}

	//删除节点
    public function node_del()
    {
        $node_id = (int)I('get.node_id', 0); if($node_id < 1){ $this->error('参数错误'); }
        $rs = $this->L_node->delNode($node_id);
        if(false === $rs){
            $this->error($this->L_node->getError());
        }else{
            $this->success('删除成功', U('node/node_list'));
        }
    }

    //节点修改
    public function node_edit()
    {
        if(IS_POST){
            $post = I('post.');
            $rs = $this->L_node->editNode($post);
            if(false === $rs){
                $this->error($this->L_node->getError());
            }else{
                $this->success('操作成功！', U('node/node_list'));
            }
            exit;
        }

        $node_id = intval(I('get.node_id'));
        if($node_id < 1){ $this->error('参数错误'); }
        $detail = $this->L_node->getNodeList(4, array('node_id'=>$node_id));
        if(empty($detail)){ $this->error('未找到数据'); }
        $detail['node_pid'] = intval($detail['node_pid']);
        //节点等级
        if($detail['node_pid'] === 0){
            $this->assign('node_parents', $this->L_node->menu_level[1]);
        }else{
            $nm = $this->L_node->getNodeList(4, array('node_id'=>$detail['node_pid']), 'node_name');
            if(empty($nm)){
                $this->assign('node_parents', '节点丢失');
            }else{
                $this->assign('node_parents', $nm['node_name']);
            }
        }
        $this->assign('detail', $detail);
        $this->assign('menuLevelSelect', $this->L_node->getMenuLevelSelect($detail['node_level']));
        $this->assign('css_styles', $this->L_node->getNodeCss());
        $this->display();
    }

    //ajax改变节点状态
    public function ajax_node_st()
    {
        $st = intval(I('request.st')); if($st!=0 && $st!=1){ $st = 0; }
        $rs = $this->L_node->updateNodeByNodeId(array('st'=>$st), I('request.node_id'));
        if(false === $rs){
            $this->ajaxReturn(genReturn('n', $this->L_node->getError()));
        }else{
            $this->ajaxReturn(genReturn('y', 'ok'));
        }
    }


}